package com.ruoyi.lvlian.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.lvlian.dto.LhsOrderDto;
import com.ruoyi.lvlian.dto.LhsOrderItemDto;
import com.ruoyi.lvlian.vo.LhsOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.lvlian.domain.LhsOrder;
import com.ruoyi.lvlian.service.ILhsOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 莱回收订单Controller
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Api(value = "莱回收-订单", tags = "莱回收订单Controller")
@RestController
@RequestMapping("/lhsOrder")
public class LhsOrderController extends BaseController {
    @Autowired
    private ILhsOrderService lhsOrderService;

    /**
     * 查询莱回收订单列表
     */
//    @RequiresPermissions("lvlian:lhsOrder:list")
    @GetMapping("/list")
    public TableDataInfo list(LhsOrderDto lhsOrder) {
        startPage();
        List<LhsOrderVo> list = lhsOrderService.selectLhsOrderList(lhsOrder);
        return getDataTable(list);
    }



    /**
     * 获取莱回收订单详细信息
     */
//    @RequiresPermissions("lvlian:lhsOrder:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(lhsOrderService.selectLhsOrderById(id));
    }



// 当日结算金额(元)  当日入库过磅单净重(kg) 当日入库过磅单数量(笔) 当日入库单数量(笔) 当日入库结算均价(元/kg)
    @ApiOperation("当日结算金额(元)")
    @GetMapping(value = "/getDailyRevenue")
    public AjaxResult getDailyRevenue(  @Validated LhsOrderDto lhsOrder) {
        return success(lhsOrderService.getDailyRevenue(lhsOrder));
    }


    @ApiOperation("当日入库结算均价(单个品类)")
    @GetMapping(value = "/getDailyAveragePrice")
    public AjaxResult getDailyAveragePrice(  @Validated LhsOrderDto lhsOrder) {
        return success(lhsOrderService.getDailyAveragePrice(lhsOrder));
    }
    /**
     * 修改莱回收订单
     */
//    @RequiresPermissions("lvlian:lhsOrder:edit")
    @Log(title = "莱回收订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LhsOrder lhsOrder) {
        return success(lhsOrderService.updateLhsOrder(lhsOrder));
    }

    /**
     * 删除莱回收订单
     */
//    @RequiresPermissions("lvlian:lhsOrder:remove")


    @PostMapping("/remove")
    @ApiOperation("莱回收订单")
    @Log(title = "莱回收订单", businessType = BusinessType.DELETE)
    public AjaxResult remove(@RequestBody String[] ids) {
        if( ids == null || ids.length == 0  ){
            return error("未选中") ;
        }
        return toAjax(lhsOrderService.deleteLhsOrderByIds(ids));
    }



    @ApiOperation("根据货品类别按照时间查询价格小程序")
    @GetMapping("/queryOrdeInfoByCategory")
    public AjaxResult queryOrdeInfoByCategory(  @Validated LhsOrderDto lhsOrder) {
/*        if (StringUtils.isEmpty(invoiceNum)) {
            AjaxResult.error("发票号码不能为空");
        }*/
        Map<String,Object> wuliu = lhsOrderService.queryOrdeInfoByCategory(lhsOrder);
        return AjaxResult.success(wuliu);
    }


    @ApiOperation("订单统计当日汇总pad")
    @GetMapping("/queryOrdeInfoSummaryByPad")
    public AjaxResult queryOrdeInfoSummaryByPad(  @Validated LhsOrderDto lhsOrder) {

        Map<String,Object> wuliu = lhsOrderService.queryOrdeInfoSummaryByPad(lhsOrder);
        return AjaxResult.success(wuliu);
    }

    @ApiOperation("当日小结按照金额大小划分数论pad")
    @GetMapping("/queryOrdeInfoByAmount")
    public AjaxResult queryOrdeInfoByAmount(  @Validated LhsOrderDto lhsOrder) {
        Map<String,Object> wuliu = lhsOrderService.queryOrdeInfoByAmount(lhsOrder);
        return AjaxResult.success(wuliu);
    }
    @ApiOperation("查询订单列表和订单项")
    @GetMapping("/getAllOrdersWithItems")
    public List<LhsOrderVo> getAllOrdersWithItems(LhsOrderDto lhsOrder) {
        return lhsOrderService.getAllOrdersWithItems(lhsOrder);
    }



    @ApiOperation("按照货品分类查询列表")
    @GetMapping(value = "/queryOrderListByProductName")
    public AjaxResult queryOrderListByProductName(  @Validated LhsOrderDto lhsOrder) {
        return success(lhsOrderService.queryOrderListByProductName(lhsOrder));
    }

    @ApiOperation("根据mac地址查询订单项，查询未结算的就是购物车内容 返回订单应该只有一个")
    @GetMapping("/queryCartOrderItemByMac")
    public AjaxResult queryCartOrderItemByMac(LhsOrderItemDto dto) {
        if(StringUtils.isEmpty(dto.getPad()) && StringUtils.isEmpty(dto.getOrderNum()) ){
            return error("pad mac或者订单num 不能同时为空");
        }

        LhsOrderVo list = lhsOrderService.queryCartOrderItemByMac(dto);
        return success(list);
    }
}
